package top.byfree.jbyan.find.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author jbyan
 * @since 2023/8/4
 */
public class BinaryFileChecker {


    public static boolean isBinaryFile(Path path) {

        String name = path.getFileName().toString().toLowerCase();

        if (name.endsWith(".exe") || name.endsWith(".bin")) {
            return true;
        }

        byte[] head = new byte[8];

        try (InputStream input = Files.newInputStream(path)) {
            int read = input.read(head);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        for (byte b : head) {
            if (b < 0x20 || b > 0x7E) {
                return true;
            }
        }

        return false;

    }

}
